package com.guchenbo.example.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class GcbFilterChain {
    protected static final Logger log = LoggerFactory.getLogger(GcbFilterChain.class);

    private List<GcbFilter> filters = new ArrayList<>();
    private int total = 0;
    private int current = 0;
    private GcbServlet servlet;

    public GcbFilterChain(GcbServlet servlet) {
        this.servlet = servlet;
    }

    public void addFilter(GcbFilter gcbFilter) {
        filters.add(gcbFilter);
        total++;
    }

    public void doFilter(GcbFilterReq req) {
        if (current < total) {
            GcbFilter filter = filters.get(current++);
            filter.doFilter(req, this);
            return;
        }
        // 全部执行完了
        servlet.service(req);
    }
}
